]> gitweb.ps.run Git - toc/blob - antlr4-cpp-runtime-4.9.2-source/demo/Mac/antlrcpp Tests/MiscClassTests.mm
add antlr source code and ReadMe
[toc] / antlr4-cpp-runtime-4.9.2-source / demo / Mac / antlrcpp Tests / MiscClassTests.mm
1 /*
2  * [The "BSD license"]
3  *  Copyright (c) 2016 Mike Lischke
4  *  All rights reserved.
5  *
6  *  Redistribution and use in source and binary forms, with or without
7  *  modification, are permitted provided that the following conditions
8  *  are met:
9  *
10  *  1. Redistributions of source code must retain the above copyright
11  *     notice, this list of conditions and the following disclaimer.
12  *  2. Redistributions in binary form must reproduce the above copyright
13  *     notice, this list of conditions and the following disclaimer in the
14  *     documentation and/or other materials provided with the distribution.
15  *  3. The name of the author may not be used to endorse or promote products
16  *     derived from this software without specific prior written permission.
17  *
18  *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #import <XCTest/XCTest.h>
31
32 #include "antlr4-runtime.h"
33
34 using namespace antlr4;
35 using namespace antlr4::misc;
36 using namespace antlrcpp;
37
38 @interface MiscClassTests : XCTestCase
39
40 @end
41
42 @implementation MiscClassTests
43
44 - (void)setUp {
45   [super setUp];
46   // Put setup code here. This method is called before the invocation of each test method in the class.
47 }
48
49 - (void)tearDown {
50   // Put teardown code here. This method is called after the invocation of each test method in the class.
51   [super tearDown];
52 }
53
54 - (void)testCPPUtils {
55
56   class A { public: virtual ~A() {}; };
57   class B : public A { public: virtual ~B() {}; };
58   class C : public A { public: virtual ~C() {}; };
59   class D : public C { public: virtual ~D() {}; };
60
61   {
62     A *a = new A(); B *b = new B(); C *c = new C(); D *d = new D();
63     XCTAssert(is<A*>(b));
64     XCTAssertFalse(is<B*>(a));
65     XCTAssert(is<A*>(c));
66     XCTAssertFalse(is<B*>(c));
67     XCTAssert(is<A*>(d));
68     XCTAssert(is<C*>(d));
69     XCTAssertFalse(is<B*>(d));
70     delete a; delete b; delete c; delete d;
71   }
72   {
73     Ref<A> a(new A());
74     Ref<B> b(new B());
75     Ref<C> c(new C());
76     Ref<D> d(new D());
77     XCTAssert(is<A>(b));
78     XCTAssertFalse(is<B>(a));
79     XCTAssert(is<A>(c));
80     XCTAssertFalse(is<B>(c));
81     XCTAssert(is<A>(d));
82     XCTAssert(is<C>(d));
83     XCTAssertFalse(is<B>(d));
84   }
85 }
86
87 - (void)testMurmurHash {
88   XCTAssertEqual(MurmurHash::initialize(), 0U);
89   XCTAssertEqual(MurmurHash::initialize(31), 31U);
90
91   // In absence of real test vectors (64bit) for murmurhash I instead check if I can find duplicate hash values
92   // in a deterministic and a random sequence of 100K values each.
93   std::set<size_t> hashs;
94   for (size_t i = 0; i < 100000; ++i) {
95     std::vector<size_t> data = { i, static_cast<size_t>(i * M_PI), arc4random() };
96     size_t hash = 0;
97     for (auto value : data)
98       hash = MurmurHash::update(hash, value);
99     hash = MurmurHash::finish(hash, data.size());
100     hashs.insert(hash);
101   }
102   XCTAssertEqual(hashs.size(), 100000U, @"At least one duplicate hash found.");
103
104   hashs.clear();
105   for (size_t i = 0; i < 100000; ++i) {
106     std::vector<size_t> data = { i, static_cast<size_t>(i * M_PI) };
107     size_t hash = 0;
108     for (auto value : data)
109       hash = MurmurHash::update(hash, value);
110     hash = MurmurHash::finish(hash, data.size());
111     hashs.insert(hash);
112   }
113   XCTAssertEqual(hashs.size(), 100000U, @"At least one duplicate hash found.");
114
115   // Another test with fixed input but varying seeds.
116   // Note: the higher the seed the less LSDs are in the result (for small input data).
117   hashs.clear();
118   std::vector<size_t> data = { L'ยต', 'a', '@', '1' };
119   for (size_t i = 0; i < 100000; ++i) {
120     size_t hash = i;
121     for (auto value : data)
122       hash = MurmurHash::update(hash, value);
123     hash = MurmurHash::finish(hash, data.size());
124     hashs.insert(hash);
125   }
126   XCTAssertEqual(hashs.size(), 100000U, @"At least one duplicate hash found.");
127 }
128
129 - (void)testInterval {
130   // The Interval class contains no error handling (checks for invalid intervals), hence some of the results
131   // look strange as we test of course such intervals as well.
132   XCTAssertEqual(Interval().length(), 0UL);
133   XCTAssertEqual(Interval(0, 0UL).length(), 1UL); // Remember: it's an inclusive interval.
134   XCTAssertEqual(Interval(100, 100UL).length(), 1UL);
135   XCTAssertEqual(Interval(-1L, -1).length(), 1UL); // Unwanted behavior: negative ranges.
136   XCTAssertEqual(Interval(-1L, -2).length(), 0UL);
137   XCTAssertEqual(Interval(100, 50UL).length(), 0UL);
138
139   XCTAssert(Interval() == Interval(-1L, -2));
140   XCTAssert(Interval(0, 0UL) == Interval(0, 0UL));
141   XCTAssertFalse(Interval(0, 1UL) == Interval(1, 2UL));
142
143   XCTAssertEqual(Interval().hashCode(), 22070U);
144   XCTAssertEqual(Interval(0, 0UL).hashCode(), 22103U);
145   XCTAssertEqual(Interval(10, 2000UL).hashCode(), 24413U);
146
147   // Results for the interval test functions in this order:
148   // startsBeforeDisjoint
149   // startsBeforeNonDisjoint
150   // startsAfter
151   // startsAfterDisjoint
152   // startsAfterNonDisjoint
153   // disjoint
154   // adjacent
155   // properlyContains
156
157   typedef std::vector<bool> TestResults;
158   struct TestEntry { size_t runningNumber; Interval interval1, interval2; TestResults results; };
159   std::vector<TestEntry> testData = {
160     // Extreme cases + invalid intervals.
161     { 0, Interval(), Interval(10, 20UL), { true, false, false, false, false, true, false, false } },
162     { 1, Interval(1, 1UL), Interval(1, 1UL), { false, true, false, false, false, false, false, true } },
163     { 2, Interval(10000, 10000UL), Interval(10000, 10000UL), { false, true, false, false, false, false, false, true } },
164     { 3, Interval(100, 10UL), Interval(100, 10UL), { false, false, false, true, false, true, false, true } },
165     { 4, Interval(100, 10UL), Interval(10, 100UL), { false, false, true, false, true, false, false, false } },
166     { 5, Interval(10, 100UL), Interval(100, 10UL), { false, true, false, false, false, false, false, true } },
167
168     // First starts before second. End varies.
169     { 20, Interval(10, 12UL), Interval(12, 100UL), { false, true, false, false, false, false, false, false } },
170     { 21, Interval(10, 12UL), Interval(13, 100UL), { true, false, false, false, false, true, true, false } },
171     { 22, Interval(10, 12UL), Interval(14, 100UL), { true, false, false, false, false, true, false, false } },
172     { 23, Interval(10, 13UL), Interval(12, 100UL), { false, true, false, false, false, false, false, false } },
173     { 24, Interval(10, 14UL), Interval(12, 100UL), { false, true, false, false, false, false, false, false } },
174     { 25, Interval(10, 99UL), Interval(12, 100UL), { false, true, false, false, false, false, false, false } },
175     { 26, Interval(10, 100UL), Interval(12, 100UL), { false, true, false, false, false, false, false, true } },
176     { 27, Interval(10, 101UL), Interval(12, 100UL), { false, true, false, false, false, false, false, true } },
177     { 28, Interval(10, 1000UL), Interval(12, 100UL), { false, true, false, false, false, false, false, true } },
178
179     // First and second start equal. End varies.
180     { 30, Interval(12, 12UL), Interval(12, 100UL), { false, true, false, false, false, false, false, false } },
181     { 31, Interval(12, 12UL), Interval(13, 100UL), { true, false, false, false, false, true, true, false } },
182     { 32, Interval(12, 12UL), Interval(14, 100UL), { true, false, false, false, false, true, false, false } },
183     { 33, Interval(12, 13UL), Interval(12, 100UL), { false, true, false, false, false, false, false, false } },
184     { 34, Interval(12, 14UL), Interval(12, 100UL), { false, true, false, false, false, false, false, false } },
185     { 35, Interval(12, 99UL), Interval(12, 100UL), { false, true, false, false, false, false, false, false } },
186     { 36, Interval(12, 100UL), Interval(12, 100UL), { false, true, false, false, false, false, false, true } },
187     { 37, Interval(12, 101UL), Interval(12, 100UL), { false, true, false, false, false, false, false, true } },
188     { 38, Interval(12, 1000UL), Interval(12, 100UL), { false, true, false, false, false, false, false, true } },
189
190     // First starts after second. End varies.
191     { 40, Interval(15, 12UL), Interval(12, 100UL), { false, false, true, false, true, false, false, false } },
192     { 41, Interval(15, 12UL), Interval(13, 100UL), { false, false, true, false, true, false, true, false } },
193     { 42, Interval(15, 12UL), Interval(14, 100UL), { false, false, true, false, true, false, false, false } },
194     { 43, Interval(15, 13UL), Interval(12, 100UL), { false, false, true, false, true, false, false, false } },
195     { 44, Interval(15, 14UL), Interval(12, 100UL), { false, false, true, false, true, false, false, false } },
196     { 45, Interval(15, 99UL), Interval(12, 100UL), { false, false, true, false, true, false, false, false } },
197     { 46, Interval(15, 100UL), Interval(12, 100UL), { false, false, true, false, true, false, false, false } },
198     { 47, Interval(15, 101UL), Interval(12, 100UL), { false, false, true, false, true, false, false, false } },
199     { 48, Interval(15, 1000UL), Interval(12, 100UL), { false, false, true, false, true, false, false, false } },
200
201     // First ends before second. Start varies.
202     { 50, Interval(10, 90UL), Interval(20, 100UL), { false, true, false, false, false, false, false, false } },
203     { 51, Interval(19, 90UL), Interval(20, 100UL), { false, true, false, false, false, false, false, false } },
204     { 52, Interval(20, 90UL), Interval(20, 100UL), { false, true, false, false, false, false, false, false } },
205     { 53, Interval(21, 90UL), Interval(20, 100UL), { false, false, true, false, true, false, false, false } },
206     { 54, Interval(98, 90UL), Interval(20, 100UL), { false, false, true, false, true, false, false, false } },
207     { 55, Interval(99, 90UL), Interval(20, 100UL), { false, false, true, false, true, false, false, false } },
208     { 56, Interval(100, 90UL), Interval(20, 100UL), { false, false, true, false, true, false, false, false } },
209     { 57, Interval(101, 90UL), Interval(20, 100UL), { false, false, true, true, false, true, true, false } },
210     { 58, Interval(1000, 90UL), Interval(20, 100UL), { false, false, true, true, false, true, false, false } },
211
212     // First and second end equal. Start varies.
213     { 60, Interval(10, 100UL), Interval(20, 100UL), { false, true, false, false, false, false, false, true } },
214     { 61, Interval(19, 100UL), Interval(20, 100UL), { false, true, false, false, false, false, false, true } },
215     { 62, Interval(20, 100UL), Interval(20, 100UL), { false, true, false, false, false, false, false, true } },
216     { 63, Interval(21, 100UL), Interval(20, 100UL), { false, false, true, false, true, false, false, false } },
217     { 64, Interval(98, 100UL), Interval(20, 100UL), { false, false, true, false, true, false, false, false } },
218     { 65, Interval(99, 100UL), Interval(20, 100UL), { false, false, true, false, true, false, false, false } },
219     { 66, Interval(100, 100UL), Interval(20, 100UL), { false, false, true, false, true, false, false, false } },
220     { 67, Interval(101, 100UL), Interval(20, 100UL), { false, false, true, true, false, true, true, false } },
221     { 68, Interval(1000, 100UL), Interval(20, 100UL), { false, false, true, true, false, true, false, false } },
222
223     // First ends after second. Start varies.
224     { 70, Interval(10, 1000UL), Interval(20, 100UL), { false, true, false, false, false, false, false, true } },
225     { 71, Interval(19, 1000UL), Interval(20, 100UL), { false, true, false, false, false, false, false, true } },
226     { 72, Interval(20, 1000UL), Interval(20, 100UL), { false, true, false, false, false, false, false, true } },
227     { 73, Interval(21, 1000UL), Interval(20, 100UL), { false, false, true, false, true, false, false, false } },
228     { 74, Interval(98, 1000UL), Interval(20, 100UL), { false, false, true, false, true, false, false, false } },
229     { 75, Interval(99, 1000UL), Interval(20, 100UL), { false, false, true, false, true, false, false, false } },
230     { 76, Interval(100, 1000UL), Interval(20, 100UL), { false, false, true, false, true, false, false, false } },
231     { 77, Interval(101, 1000UL), Interval(20, 100UL), { false, false, true, true, false, true, true, false } },
232     { 78, Interval(1000, 1000UL), Interval(20, 100UL), { false, false, true, true, false, true, false, false } },
233
234     // It's possible to add more tests with borders that touch each other (e.g. first starts before/on/after second
235     // and first ends directly before/after second. However, such cases are not handled differently in the Interval
236     // class
237     // (only adjacent intervals, where first ends directly before second starts and vice versa. So I ommitted them here.
238   };
239
240   for (auto &entry : testData) {
241     XCTAssert(entry.interval1.startsBeforeDisjoint(entry.interval2) == entry.results[0], @"entry: %zu",
242               entry.runningNumber);
243     XCTAssert(entry.interval1.startsBeforeNonDisjoint(entry.interval2) == entry.results[1], @"entry: %zu",
244               entry.runningNumber);
245     XCTAssert(entry.interval1.startsAfter(entry.interval2) == entry.results[2], @"entry: %zu", entry.runningNumber);
246     XCTAssert(entry.interval1.startsAfterDisjoint(entry.interval2) == entry.results[3], @"entry: %zu",
247               entry.runningNumber);
248     XCTAssert(entry.interval1.startsAfterNonDisjoint(entry.interval2) == entry.results[4], @"entry: %zu",
249               entry.runningNumber);
250     XCTAssert(entry.interval1.disjoint(entry.interval2) == entry.results[5], @"entry: %zu", entry.runningNumber);
251     XCTAssert(entry.interval1.adjacent(entry.interval2) == entry.results[6], @"entry: %zu", entry.runningNumber);
252     XCTAssert(entry.interval1.properlyContains(entry.interval2) == entry.results[7], @"entry: %zu",
253               entry.runningNumber);
254   }
255
256   XCTAssert(Interval().Union(Interval(10, 100UL)) == Interval(-1L, 100));
257   XCTAssert(Interval(10, 10UL).Union(Interval(10, 100UL)) == Interval(10, 100UL));
258   XCTAssert(Interval(10, 11UL).Union(Interval(10, 100UL)) == Interval(10, 100UL));
259   XCTAssert(Interval(10, 1000UL).Union(Interval(10, 100UL)) == Interval(10, 1000UL));
260   XCTAssert(Interval(1000, 30UL).Union(Interval(10, 100UL)) == Interval(10, 100UL));
261   XCTAssert(Interval(1000, 2000UL).Union(Interval(10, 100UL)) == Interval(10, 2000UL));
262   XCTAssert(Interval(500, 2000UL).Union(Interval(10, 1000UL)) == Interval(10, 2000UL));
263
264   XCTAssert(Interval().intersection(Interval(10, 100UL)) == Interval(10, -2L));
265   XCTAssert(Interval(10, 10UL).intersection(Interval(10, 100UL)) == Interval(10, 10UL));
266   XCTAssert(Interval(10, 11UL).intersection(Interval(10, 100UL)) == Interval(10, 11UL));
267   XCTAssert(Interval(10, 1000UL).intersection(Interval(10, 100UL)) == Interval(10, 100UL));
268   XCTAssert(Interval(1000, 30UL).intersection(Interval(10, 100UL)) == Interval(1000, 30UL));
269   XCTAssert(Interval(1000, 2000UL).intersection(Interval(10, 100UL)) == Interval(1000, 100UL));
270   XCTAssert(Interval(500, 2000UL).intersection(Interval(10, 1000UL)) == Interval(500, 1000UL));
271
272   XCTAssert(Interval().toString() == "-1..-2");
273   XCTAssert(Interval(10, 10UL).toString() == "10..10");
274   XCTAssert(Interval(1000, 2000UL).toString() == "1000..2000");
275   XCTAssert(Interval(500UL, INT_MAX).toString() == "500.." + std::to_string(INT_MAX));
276 }
277
278 - (void)testIntervalSet {
279   XCTAssertFalse(IntervalSet().isReadOnly());
280   XCTAssert(IntervalSet().isEmpty());
281
282   IntervalSet set1;
283   set1.setReadOnly(true);
284   XCTAssert(set1.isReadOnly());
285
286   XCTAssert(IntervalSet() == IntervalSet::EMPTY_SET);
287
288   std::vector<Interval> intervals = { Interval(), Interval(10, 20UL), Interval(20, 100UL), Interval(1000, 2000UL) };
289   IntervalSet set2(intervals);
290   XCTAssertFalse(set2.isEmpty());
291   XCTAssertFalse(set2.contains(9UL));
292   XCTAssert(set2.contains(10UL));
293   XCTAssert(set2.contains(20UL));
294   XCTAssertTrue(set2.contains(22UL));
295   XCTAssert(set2.contains(1111UL));
296   XCTAssertFalse(set2.contains(10000UL));
297   XCTAssertEqual(set2.getSingleElement(), Token::INVALID_TYPE);
298   XCTAssertEqual(set2.getMinElement(), -1);
299   XCTAssertEqual(set2.getMaxElement(), 2000);
300
301   IntervalSet set3(set2);
302   XCTAssertFalse(set3.isEmpty());
303   XCTAssertFalse(set3.contains(9UL));
304   XCTAssert(set3.contains(10UL));
305   XCTAssert(set3.contains(20UL));
306   XCTAssertTrue(set3.contains(22UL));
307   XCTAssert(set3.contains(1111UL));
308   XCTAssertFalse(set3.contains(10000UL));
309   XCTAssertEqual(set3.getSingleElement(), Token::INVALID_TYPE);
310   XCTAssertEqual(set3.getMinElement(), 10);
311   XCTAssertEqual(set3.getMaxElement(), 2000);
312
313   set3.add(Interval(100, 1000UL));
314   XCTAssertEqual(set3.getMinElement(), 10);
315   set3.add(Interval(9, 1000UL));
316   XCTAssertEqual(set3.getMinElement(), 9);
317   set3.add(Interval(1, 1UL));
318   XCTAssertEqual(set3.getMinElement(), 1);
319
320   IntervalSet set4;
321   set4.add(10);
322   XCTAssertEqual(set4.getSingleElement(), 10);
323   XCTAssertEqual(set4.getMinElement(), 10);
324   XCTAssertEqual(set4.getMaxElement(), 10);
325
326   set4.clear();
327   XCTAssert(set4.isEmpty());
328   set4.add(Interval(10, 10UL));
329   XCTAssertEqual(set4.getSingleElement(), 10);
330   XCTAssertEqual(set4.getMinElement(), 10);
331   XCTAssertEqual(set4.getMaxElement(), 10);
332   set4.setReadOnly(true);
333   try {
334     set4.clear();
335     XCTFail(@"Expected exception");
336   } catch (IllegalStateException &e) {
337   }
338
339   try {
340     set4.setReadOnly(false);
341     XCTFail(@"Expected exception");
342   } catch (IllegalStateException &e) {
343   }
344
345   try {
346     set4 = IntervalSet::of(12345);
347     XCTFail(@"Expected exception");
348   } catch (IllegalStateException &e) {
349   }
350
351   IntervalSet set5 = IntervalSet::of(12345);
352   XCTAssertEqual(set5.getSingleElement(), 12345);
353   XCTAssertEqual(set5.getMinElement(), 12345);
354   XCTAssertEqual(set5.getMaxElement(), 12345);
355
356   IntervalSet set6(10, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50);
357   XCTAssertEqual(set6.getMinElement(), 5);
358   XCTAssertEqual(set6.getMaxElement(), 50);
359   XCTAssertEqual(set6.size(), 10U);
360   set6.add(12, 18);
361   XCTAssertEqual(set6.size(), 16U); // (15, 15) replaced by (12, 18)
362   set6.add(9, 33);
363   XCTAssertEqual(set6.size(), 30U); // (10, 10), (12, 18), (20, 20), (25, 25) and (30, 30) replaced by (9, 33)
364
365   XCTAssert(IntervalSet(3, 1, 2, 10).Or(IntervalSet(3, 1, 2, 5)) == IntervalSet(4, 1, 2, 5, 10));
366   XCTAssert(IntervalSet({ Interval(2, 10UL) }).Or(IntervalSet({ Interval(5, 8UL) })) == IntervalSet({ Interval(2, 10UL) }));
367
368   XCTAssert(IntervalSet::of(1, 10).complement(IntervalSet::of(7, 55)) == IntervalSet::of(11, 55));
369   XCTAssert(IntervalSet::of(1, 10).complement(IntervalSet::of(20, 55)) == IntervalSet::of(20, 55));
370   XCTAssert(IntervalSet::of(1, 10).complement(IntervalSet::of(5, 6)) == IntervalSet::EMPTY_SET);
371   XCTAssert(IntervalSet::of(15, 20).complement(IntervalSet::of(7, 55)) ==
372             IntervalSet({ Interval(7, 14UL), Interval(21, 55UL) }));
373   XCTAssert(IntervalSet({ Interval(1, 10UL), Interval(30, 35UL) }).complement(IntervalSet::of(7, 55)) ==
374             IntervalSet({ Interval(11, 29UL), Interval(36, 55UL) }));
375
376   XCTAssert(IntervalSet::of(1, 10).And(IntervalSet::of(7, 55)) == IntervalSet::of(7, 10));
377   XCTAssert(IntervalSet::of(1, 10).And(IntervalSet::of(20, 55)) == IntervalSet::EMPTY_SET);
378   XCTAssert(IntervalSet::of(1, 10).And(IntervalSet::of(5, 6)) == IntervalSet::of(5, 6));
379   XCTAssert(IntervalSet::of(15, 20).And(IntervalSet::of(7, 55)) == IntervalSet::of(15, 20));
380
381   XCTAssert(IntervalSet::of(1, 10).subtract(IntervalSet::of(7, 55)) == IntervalSet::of(1, 6));
382   XCTAssert(IntervalSet::of(1, 10).subtract(IntervalSet::of(20, 55)) == IntervalSet::of(1, 10));
383   XCTAssert(IntervalSet::of(1, 10).subtract(IntervalSet::of(5, 6)) ==
384             IntervalSet({ Interval(1, 4UL), Interval(7, 10UL) }));
385   XCTAssert(IntervalSet::of(15, 20).subtract(IntervalSet::of(7, 55)) == IntervalSet::EMPTY_SET);
386 }
387
388 @end